package com.chinasoft.movie.controller;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.*;

public class BaseController {

    public Map<String,Object> getParamMap(HttpServletRequest request){
        Map<String,String[]> map=request.getParameterMap();
        Set<String> set=map.keySet();
        Iterator<String> it=set.iterator();
        Map<String,Object> paramMap=new HashMap<String,Object>();
        while(it.hasNext()){
            String key=it.next();//得到所有的key
            String [] s=map.get(key);
            //System.out.println(s[0]);
            paramMap.put(key,s[0]);
        }
        return paramMap;
    }

    public List<Integer> getParamIdsList(HttpServletRequest request){
        //int count=userService.insertUser(getParamMap(request));
        // String ids=request.getParameter("ids");
        StringBuffer buff=new StringBuffer(request.getParameter("ids"));
        buff.delete(buff.length()-1,buff.length());
        List<Integer> list=new ArrayList<Integer>();
        String s[]=buff.toString().split(",");
        for (int i=0;i<s.length;i++){
            list.add(Integer.parseInt(s[i]));
        }
        return list;
    }

//用来组装除了List之外的分页参数
    public Map<String,Object> getSelectTableList(int totalCount,Map<String,Object> paramMap){
        if(paramMap.get("pageIndex")!=null&&paramMap.get("pageSize")!=null){
            //int totalCount=userService.selectUserCount(paramMap);//根据条件的内容进行数据的个数的查询
            int pageIndex=Integer.parseInt(paramMap.get("pageIndex").toString());
            int pageSize=Integer.parseInt(paramMap.get("pageSize").toString());
            int totalPage=totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
            int start=(pageIndex-1)*pageSize;//分页的起始索引号
            paramMap.put("totalCount",totalCount);
            paramMap.put("pageSize",pageSize);
            paramMap.put("totalPage",totalPage);
            paramMap.put("start",start);
        }
        return paramMap;
    }

//    public  String  upload(@RequestParam("attr") MultipartFile file, HttpServletRequest request){
//        FileOutputStream out = null;//声明文件输出流
//        InputStream in = null;//声明输入流
//        String newFileName = null;
//        try{
//            String fileName=file.getOriginalFilename();//得到文件的名称
//            //System.out.println(fileName);
//            String suffixName=fileName.substring(fileName.lastIndexOf("."));//文件的后缀名
//             newFileName="/img/"+UUID.randomUUID().toString()+suffixName;
//            String filePath = "d:\\"+newFileName;//新的路径以及文件名称
//            out=new FileOutputStream(filePath);
//            in=file.getInputStream();
//            IOUtils.copy(in,out);
//        }catch (Exception e){
//
//        }finally {
//            try{
//                in.close();
//                out.close();
//            }catch (Exception e){
//            }
//        }
//        return newFileName;
//    }

    public  Object[] upload(@RequestParam("attr") List<MultipartFile> file, HttpServletRequest request){
        FileOutputStream out = null;//声明文件输出流
        InputStream in = null;//声明输入流
        String newFileName = null;
        List<String> list=new ArrayList<String>();//文件的地址
        List<Long> dataList=new ArrayList<Long>();//每个文件的大小
        try{
          for(int i=0;i<file.size();i++){
              String fileName=file.get(i).getOriginalFilename();//得到文件的名称
              String suffixName=fileName.substring(fileName.lastIndexOf("."));//文件的后缀名
              newFileName="/img/"+UUID.randomUUID().toString()+suffixName;
              String filePath = "d:\\"+newFileName;//新的路径以及文件名称
              out=new FileOutputStream(filePath);
              in=file.get(i).getInputStream();
              IOUtils.copy(in,out);
              list.add(newFileName);
              dataList.add(file.get(i).getSize());
          }
        }catch (Exception e){

        }finally {
            try{
                in.close();
                out.close();
            }catch (Exception e){
            }
        }
        return new Object[]{list,dataList};
    }

    //将记事本中的数据读取到List里面，然后通过控制 for int i=0;i<5;i++  int i=5 i<10;i++

    //pojo 1-3 页
    //pageIndex if result==false pageIndex==1||pageIndex==2||pageIndex==3

    //序列化
    //ServerSocket IO


}
